package xlj.lib.android.base.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import xlj.lib.android.base.R;


public class DialogUtil {

    public static class CustomBuilder {
        private Dialog dialog;
        private View view;
        public CustomBuilder(Activity context,int layoutResId) {
            this.dialog = createDialog(context,layoutResId);
        }

        public CustomBuilder(Activity context,View view) {
            this.dialog = createDialog(context,view);
        }

        public static CustomBuilder createBuilder(@NonNull Activity activity,View view) {
            return new CustomBuilder(activity,view);
        }

        private Dialog createDialog(@NonNull Activity context,View view) {
            final AlertDialog dialog = new AlertDialog.Builder(context).setView(view).create();
            this.view = view;
            return dialog;
        }

        public static CustomBuilder createBuilder(@NonNull Activity activity,int layoutResId) {
            return new CustomBuilder(activity,layoutResId);
        }
        private Dialog createDialog(@NonNull Activity context,int layoutResId) {
            final View view = LayoutInflater.from(context).inflate(
                    layoutResId, null);
            final AlertDialog dialog = new AlertDialog.Builder(context).setView(view).create();
            this.view = view;
            return dialog;
        }

        public CustomBuilder show() {
            if (this.dialog != null) {
                dialog.show();
            }
            return this;
        }

        public CustomBuilder dismiss() {
            if (this.dialog != null) {
                dialog.dismiss();
            }
            return this;
        }

        public CustomBuilder setViewClick(int viewId, View.OnClickListener clickListener) {
            View view = findViewById(viewId);
            if (view != null) {
                view.setOnClickListener(clickListener);
            }
            return this;
        }

        public CustomBuilder setCancelable(boolean cancelable) {
            if (dialog != null) {
                dialog.setCancelable(cancelable);
            }
            return this;
        }

        public Dialog build() {
            return this.dialog;
        }

        public View findViewById(int resId) {
            if (view != null) {
                return view.findViewById(resId);
            }
            return null;
        }
    }

    public static class  DialogBuilder {
        private Dialog dialog;
        private TextView tv_title;
        private TextView tv_content;
        private TextView cancel;
        private TextView sure;

        public DialogBuilder(Activity context) {
            this.dialog = createDialog(context);
        }

        public static DialogBuilder createBuilder(@NonNull Activity activity) {
            return new DialogBuilder(activity);
        }

        private Dialog createDialog(@NonNull Activity context) {
            final View view = LayoutInflater.from(context).inflate(
                    R.layout.dialog_custom, null);
            final AlertDialog dialog = new AlertDialog.Builder(context).setView(view).create();

            this.tv_title = (TextView) view.findViewById(R.id.alert_title);

            this.tv_content = (TextView) view.findViewById(R.id.alert_content);

            this.cancel = (TextView) view.findViewById(R.id.cancel);

            sure = (TextView) view.findViewById(R.id.ok);

            cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            return dialog;
        }

        public DialogBuilder setTitle(String title) {
            tv_title.setText(title);
            return this;
        }

        public DialogBuilder centerContent() {
            tv_content.setGravity(Gravity.CENTER);
            return this;
        }

        public DialogBuilder setContentGravity(int gravity) {
            tv_content.setGravity(gravity);
            return this;
        }

        public DialogBuilder noTitle() {
            tv_title.setVisibility(View.GONE);
            return this;
        }

        public DialogBuilder setContent(String content) {
            tv_content.setText(content);
            return this;
        }

        public DialogBuilder setSureText(String text) {
            sure.setText(text);
            return this;
        }

        public DialogBuilder setCancelText(String text) {
            cancel.setText(text);
            return this;
        }

        public DialogBuilder noCancel() {
            cancel.setVisibility(View.GONE);
            return this;
        }

        public DialogBuilder noSure() {
            sure.setVisibility(View.GONE);
            return this;
        }

        public DialogBuilder setTouchOutside(boolean touchOutside) {
            dialog.setCanceledOnTouchOutside(touchOutside);
            return this;
        }

        public DialogBuilder setCancelable(boolean cancelable) {
            dialog.setCancelable(cancelable);
            return this;
        }

        public DialogBuilder setOnSureListener(final View.OnClickListener onClickListener) {
            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (onClickListener != null) {
                        onClickListener.onClick(v);
                    }
                }
            });
            return this;
        }

        public Dialog build() {
            return this.dialog;
        }
    }

}
